package com.example.administrator.mynotiifys;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import com.blankj.utilcode.util.EncodeUtils;
import com.blankj.utilcode.util.StringUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URLEncoder;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

public class Snotiifys extends NotificationListenerService {

    @Override
    public void onCreate() {
        super.onCreate();
        final String flat = Settings.Secure.getString(getContentResolver(),
                "enabled_notification_listeners");

        if(StringUtils.isEmpty(flat)) {
            Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        try
        {
            Notification n = sbn.getNotification();
            if (n == null) {
                return;
            }
            // 标题和时间
            String title = "";
            if (n.tickerText != null) {
                title = n.tickerText.toString();
            }
            long when = n.when;
            // 其它的信息存在一个bundle中，此bundle在android4.3及之前是私有的，需要通过反射来获取；android4.3之后可以直接获取
            Bundle bundle = null;
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2) {
                // android 4.3
                try {
                    Field field = Notification.class.getDeclaredField("extras");
                    bundle = (Bundle) field.get(n);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
                // android 4.3之后
                bundle = n.extras;
            }
            // 内容标题、内容、副内容
            String contentTitle = bundle.getString(Notification.EXTRA_TITLE);
            if (contentTitle == null) {
                contentTitle = "";
            }
            String contentText = bundle.getString(Notification.EXTRA_TEXT);
            if (contentText == null) {
                contentText = "";
            }
            String contentSubtext = bundle.getString(Notification.EXTRA_SUB_TEXT);
            if (contentSubtext == null) {
                contentSubtext = "";
            }
            OkHttpClient client = new OkHttpClient();
            String para="?text="+ EncodeUtils.urlEncode(contentTitle)+"&desp="+EncodeUtils.urlEncode(contentText+"   "+contentSubtext);
            Request request=new Request.Builder().url("数据接口地址替换为自己的"+para)
                    .build();

            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    //
                    Log.d("Snotiifys",e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //
                    Log.d("Snotiifys",response.body().toString());
                }
            });
            //Response response = client.newCall(request).execute();
//            return response.body().string();

            Log.d("wanghang", "notify msg: title=" + title + " ,when=" + when
                    + " ,contentTitle=" + contentTitle + " ,contentText="
                    + contentText + " ,contentSubtext=" + contentSubtext);

        }
        catch (Exception ex)
        {
            Log.e("onNotificationPosted",ex.getMessage());
        }
        finally {
            super.onNotificationPosted(sbn);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
